package com.liang.text;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Text {
    public static void main(String[] args) {
        //通过键盘录入两个user对象的信息，封装到user对象，将user对象保存到集合中
        Scanner sc = new Scanner(System.in);
        ArrayList<User> list = new ArrayList<>();
        addUser(sc, list);

        //通过字符输出流，将集合中每一个user对象的属性写出到D:\\user.txt文件中
        write(list);

    }

    public static void write(ArrayList<User> list) {
        FileWriter fw = null;

        try {
            fw = new FileWriter("D:\\user.txt");

            for (User user : list) {
                fw.write(user.getUid() + "," + user.getName() + "," + user.getAge() + "\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static void addUser(Scanner sc, ArrayList<User> list) {
        for (int i = 1; i <= 2; i++) {
            System.out.println("请输入第" + i + "个对象的信息（编号，姓名。年龄）");
            String s = sc.nextLine();
            String[] mes = s.split(",");
            System.out.println(mes.length);
            User user = new User(mes[0], mes[1], mes[2]);
            list.add(user);

        }
    }
}

